package com.exam.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exam.pojo.*;
import com.exam.service.HandPaperService;
import com.exam.service.PaperService;
import com.exam.service.QuestionService;
import com.exam.service.UserPaperService;
import com.exam.vo.ExamPaperVO;
import com.exam.vo.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XiaoKai
 * @since 2021-07-31
 */
@RestController
@RequestMapping("/exam")
public class UserPaperController extends BaseController {
    @Autowired
    private UserPaperService userPaperService;
    @Autowired
    private PaperService paperService;
    @Autowired
    private HandPaperService handPaperService;
    @Autowired
    private QuestionService questionService;
    @GetMapping("/list")
    public ResultData<Map<String, Object>> paperList(){
        User user = getSessionUser();
//        List<UserPaper> examList = userPaperService.examList(user.getId());

        QueryWrapper queryWrapper = new QueryWrapper();
        ArrayList examList = new ArrayList();
        queryWrapper.eq("user_id",user.getId());
        HashMap<String, Object> examListMap = new HashMap<>();
        List<HandPaper> list = handPaperService.getBaseMapper().selectList(queryWrapper);
            for (HandPaper o : list) {
                Integer paperQuestionId = o.getPaperQuestionId();
                queryWrapper.clear();
                queryWrapper.eq("id",paperQuestionId);
                Paper paper = paperService.getBaseMapper().selectOne(queryWrapper);
                paper.setHandPaper(o);
                examList.add(paper);
            }
        examListMap.put("examList",examList);
        return new ResultData<Map<String, Object>>(examListMap);
    }
    @GetMapping("/onlineExam")
    public ResultData<Map<String, Object>> onlineExam(@RequestParam("examId") Integer id){
        System.out.println(id);
        ExamPaperVO examPaper = questionService.getExamPaper(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("examPaper",examPaper);
        return new ResultData(map);

    }
    @GetMapping("/questionManage")
    public ResultData<Map<String, Object>> manageQuestion(){
        QueryWrapper queryWrapper = new QueryWrapper();
        List list = questionService.getBaseMapper().selectList(queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("questionList",list);
        return new ResultData<Map<String, Object>>(map);
    }
    public User getSessionUser(){
        HttpSession session = getSession();
        User user = (User) session.getAttribute(SESSION_KEY_LOGIN_USER);
        return user;
    }
}

